/*
 * @(#) NewsContoller.java 1.0  2011-8-17 
 *
 * (c) Copyright 2011 smshen S3H3L3
 *
 * FILENAME    : NewsContoller.java
 * PACKAGE     : com.sm.web
 * CREATE DATE : 2011-8-17
 * AUTHOR      : smshen
 */

package com.sm.web;

import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.sm.lucene.SearchNews;
import com.sm.model.News;
import com.sm.model.UserInfo;
import com.sm.service.NewsService;

/*******************************************************************************
 *  #(c) smshen S3H3L3 
 *
 *  功能说明: 
  
 *  2011-8-17 下午5:08:49 smshen 创建文件
 * 
 *  修改说明: 创建文件

 *  2011-8-17 下午5:08:49 smshen 修改文件
 * 
 ******************************************************************************/

@Controller
public class NewsContoller {
	
	/**
	 * JSON 
	 */
	private static String JSON = "json";
	
	/**
	 * THIRTY 30
	 */
	private static final int THIRTY = 30;

	/**
	 * newsService 新闻服务管理
	 */
	@Autowired
	private NewsService newsService;
	
	/**
	 * 
	 * 功能 :添加新闻
	
	 * 开发：smshen 2011-8-23
	
	 * @param request HttpServletRequest
	 * @param session HttpSession
	 * @param news 新闻实体
	 * @return ModelAndView
	 */
	@RequestMapping("/news/addNews.do")
	public ModelAndView addNews(HttpServletRequest request, HttpSession session, News news) {
		ModelAndView mv = new ModelAndView("news/newsList");
		UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		news.setAuthor(userInfo.getUserName());
		news.setTime(dateFormat.format(new Date()));
		String content = news.getContent();
		String digest = content.length() > THIRTY ? content.substring(0, THIRTY) : content;
		news.setDigest(digest);
		newsService.addNews(news);
		return mv;
	}
	
	/**
	 * 
	 * 功能 :获取新闻列表
	
	 * 开发：smshen 2011-8-23
	
	 * @param request HttpServletRequest
	 * @return ModelAndView
	 */
	@RequestMapping("/news/getAllnewsList.do")
	public ModelAndView getAllnewsList(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView(JSON);
		mv.addObject(JSON, newsService.getAllNews(request));
		return mv;
	}
	
	/**
	 * 
	 * 功能 :全文检索
	
	 * 开发：smshen 2011-8-23
	
	 * @param request HttpServletRequest
	 * @return ModelAndView
	 */
	@RequestMapping("/news/fullTextSearch.do") 
	public ModelAndView fullTextSearch(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView(JSON);
		String condition = null;
		try {
			condition = new String(request.getParameter("condition").getBytes("iso8859-1"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} 
		mv.addObject(JSON, SearchNews.searchNews(condition));
		return mv;
	}
}


